package com.zwface.demo.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

/**
 * created by 张中文
 * on 2020/2/3
 * 描述：
 */
public abstract class FrameFragment<T extends ViewDataBinding> extends Fragment
{
    public T binding;
    protected FrameActivity context;
    protected View rootView;
    protected boolean isInit = false;
    private boolean isRegister = false;

    protected abstract int onLayoutId();

    protected abstract void onInitBinding(T binding);

    protected abstract boolean onSetEventBus();

    protected abstract void onInitData(Bundle savedInstanceState);

    protected abstract void onRefreshUi();

    protected abstract void onInitComponent();

    protected abstract void onInitListener();

    public void registerEventBus() {
        isRegister = true;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!isInit) {
            context = (FrameActivity) this.getActivity();
            onInitData(savedInstanceState);
            onInitComponent();
            onInitListener();
            isInit = true;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        onRefreshUi();
        if (onSetEventBus() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = setContentView(inflater, onLayoutId());
        return view;
    }

    protected FrameActivity getFrameActivity() {
        return (FrameActivity) getActivity();
    }

    public View getContentView(){
        return rootView;
    }

    /**
     * 调用该办法可避免重复加载UI
     */
    public View setContentView(LayoutInflater inflater, int resId) {
        if (rootView == null) {
            binding = DataBindingUtil.inflate(inflater, resId, null, false);
            rootView = binding.getRoot();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        onInitBinding(binding);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (onSetEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 跳转到一个新的Activity，不结束当前的Activity
     *
     * @param c
     * @param bundle
     */
    public void gotoActivityNotClose(Class<?> c, Bundle bundle) {
        //防止快速点击两次
        if (isDoubleClick(c.getName())) {
            return;
        }

        Intent i = new Intent(this.context, c);
        if (bundle != null) {
            i.putExtras(bundle);
        }
        this.startActivity(i);
    }

    /**
     * 跳转到一个新的Activity，并将当前的Activity结束
     *
     * @param c
     * @param bundle
     */
    public void gotoActivity(Class<?> c, Bundle bundle) {
        //防止快速点击两次
        if (isDoubleClick(c.getName())) {
            return;
        }

        Intent i = new Intent(this.context, c);
        if (bundle != null) {
            i.putExtras(bundle);
        }
        this.startActivity(i);
        this.getActivity().finish();
    }

    /**
     * 跳转到一个Activity，需要回调结果
     *
     * @param requestCode
     * @param c
     * @param bundle
     */
    public void gotoActivityForResult(int requestCode, Class<?> c, Bundle bundle) {
        //防止快速点击两次
        if (isDoubleClick(c.getName())) {
            return;
        }

        Intent i = new Intent(this.context, c);
        if (bundle != null) {
            i.putExtras(bundle);
        }
        startActivityForResult(i, requestCode);
    }

    /**
     * 防止快速跳转两次
     */
    private HashMap requestTimeStampHashMap = new HashMap<String, Long>(); //上一次请求时间
    private Long limitRequestTime = 600L;  //400毫秒

    public boolean isDoubleClick(String requestMethodName) {      //防止用户连续点击按钮调用
        if (!requestTimeStampHashMap.containsKey(requestMethodName)) {
            requestTimeStampHashMap.put(requestMethodName, 0L);
        }


        Long preTimeStamp = (Long) requestTimeStampHashMap.get(requestMethodName);
        if ((System.currentTimeMillis() - preTimeStamp) > limitRequestTime) {
            requestTimeStampHashMap.put(requestMethodName, System.currentTimeMillis());
            return false;
        } else {
            return true;
        }
    }
}
